using JSOCTNetSDK.XmppTaskModel;
using JSST.Framework.DBAccess;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace JSOCTNetSDK.XmppTask
{
	public class TaskInfoFun
	{
		public JSOCTNetSDK.XmppTaskModel.TaskInfo GetModel(int ID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 * from SYNC.TaskInfo ");
			stringBuilder.Append(" where ID=@ID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ID", SqlDbType.Int, 4)
			};
			array[0].Value = ID;
			new TaskInfo();
			DataTable dataTableText = MSSQL.GetDataTableText(stringBuilder.ToString(), array);
			if (dataTableText.Rows.Count > 0)
			{
				return this.DataRowToModel(dataTableText.Rows[0]);
			}
			return null;
		}

		public int Add(JSOCTNetSDK.XmppTaskModel.TaskInfo model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into SYNC.TaskInfo(");
			stringBuilder.Append("TaskName,TaskCode,TaskType,ClassName,DllPath,VersionID,CornStr,Enable,IsCache,CacheTime,IsSyncData)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@TaskName,@TaskCode,@TaskType,@ClassName,@DllPath,@VersionID,@CornStr,@Enable,@IsCache,@CacheTime,@IsSyncData)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@TaskName", SqlDbType.VarChar, 100),
				new SqlParameter("@TaskCode", SqlDbType.VarChar, 100),
				new SqlParameter("@TaskType", SqlDbType.Int, 4),
				new SqlParameter("@ClassName", SqlDbType.VarChar, 100),
				new SqlParameter("@DllPath", SqlDbType.VarChar, 100),
				new SqlParameter("@VersionID", SqlDbType.Int, 4),
				new SqlParameter("@CornStr", SqlDbType.VarChar, 50),
				new SqlParameter("@Enable", SqlDbType.Bit, 1),
				new SqlParameter("@IsCache", SqlDbType.Bit, 1),
				new SqlParameter("@CacheTime", SqlDbType.Int, 4),
				new SqlParameter("@IsSyncData", SqlDbType.Bit, 1)
			};
			array[0].Value = model.TaskName;
			array[1].Value = model.TaskCode;
			array[2].Value = model.TaskType;
			array[3].Value = model.ClassName;
			array[4].Value = model.DllPath;
			array[5].Value = model.VersionID;
			array[6].Value = model.CornStr;
			array[7].Value = model.Enable;
			array[8].Value = model.IsCache;
			array[9].Value = model.CacheTime;
			array[10].Value = model.IsSyncData;
			object obj = MSSQL.ExecuteScalarText(stringBuilder.ToString(), array);
			if (obj == null)
			{
				return 0;
			}
			return System.Convert.ToInt32(obj);
		}

		public bool Update(JSOCTNetSDK.XmppTaskModel.TaskInfo model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update SYNC.TaskInfo set ");
			stringBuilder.Append("TaskName=@TaskName,");
			stringBuilder.Append("TaskCode=@TaskCode,");
			stringBuilder.Append("TaskType=@TaskType,");
			stringBuilder.Append("ClassName=@ClassName,");
			stringBuilder.Append("DllPath=@DllPath,");
			stringBuilder.Append("VersionID=@VersionID,");
			stringBuilder.Append("CornStr=@CornStr,");
			stringBuilder.Append("Enable=@Enable,");
			stringBuilder.Append("IsCache=@IsCache,");
			stringBuilder.Append("CacheTime=@CacheTime,");
			stringBuilder.Append("IsSyncData=@IsSyncData");
			stringBuilder.Append(" where ID=@ID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@TaskName", SqlDbType.VarChar, 100),
				new SqlParameter("@TaskCode", SqlDbType.VarChar, 100),
				new SqlParameter("@TaskType", SqlDbType.Int, 4),
				new SqlParameter("@ClassName", SqlDbType.VarChar, 100),
				new SqlParameter("@DllPath", SqlDbType.VarChar, 100),
				new SqlParameter("@VersionID", SqlDbType.Int, 4),
				new SqlParameter("@CornStr", SqlDbType.VarChar, 50),
				new SqlParameter("@Enable", SqlDbType.Bit, 1),
				new SqlParameter("@IsCache", SqlDbType.Bit, 1),
				new SqlParameter("@CacheTime", SqlDbType.Int, 4),
				new SqlParameter("@IsSyncData", SqlDbType.Bit, 1),
				new SqlParameter("@ID", SqlDbType.Int, 4)
			};
			array[0].Value = model.TaskName;
			array[1].Value = model.TaskCode;
			array[2].Value = model.TaskType;
			array[3].Value = model.ClassName;
			array[4].Value = model.DllPath;
			array[5].Value = model.VersionID;
			array[6].Value = model.CornStr;
			array[7].Value = model.Enable;
			array[8].Value = model.IsCache;
			array[9].Value = model.CacheTime;
			array[10].Value = model.IsSyncData;
			array[11].Value = model.ID;
			int num = MSSQL.ExecuteNonQueryText(stringBuilder.ToString(), array);
			return num > 0;
		}

		public bool EnableTask(int TaskID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("Update SYNC.TaskInfo set enable = 1 where id =" + TaskID);
			int num = MSSQL.ExecuteNonQueryText(stringBuilder.ToString(), null);
			return num > 0;
		}

		public bool Disable(int TaskID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("Update SYNC.TaskInfo set enable = 0 where id =" + TaskID);
			int num = MSSQL.ExecuteNonQueryText(stringBuilder.ToString(), null);
			return num > 0;
		}

		public bool DisableAll()
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("Update SYNC.TaskInfo set enable = 0 where taskcode in ('NISSP_ACION_EQUIPMENT_STATE'\r\n,'NISSP_PARK_CAR_IN','NISSP_PARK_SPACE','NISSP_PARK_IN','NISSP_PARK_OUT','NISSP_PARK_DISCOUNT','dm.park.ticketdiscount','NISSP_DOOR_INOUT')");
			int num = MSSQL.ExecuteNonQueryText(stringBuilder.ToString(), null);
			return num > 0;
		}

		public bool Delete(int ID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from SYNC.TaskInfo ");
			stringBuilder.Append(" where ID=@ID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@ID", SqlDbType.Int, 4)
			};
			array[0].Value = ID;
			int num = MSSQL.ExecuteNonQueryText(stringBuilder.ToString(), array);
			return num > 0;
		}

		public DataTable GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ROW_NUMBER() OVER ( ORDER BY [Enable] desc,[ID] ) AS RowNo,* From (\r\n                            select a.ID,b.VersionName,a.TaskName,a.TaskCode,\r\n                            CASE a.TaskType WHEN 1 THEN '下发' ELSE '' END AS TaskDown,\r\n                            CASE a.IsCache WHEN 1 THEN '上传' ELSE '' END  AS TaskUp,\r\n                            CASE a.IsSyncData WHEN 1 THEN '同步' ELSE '' END AS TaskUpHistory,\r\n                            --a.ClassName,a.DllPath,\r\n                            a.CornStr,CASE a.Enable WHEN 1 THEN '启用' ELSE '禁用' END AS Enable ");
			stringBuilder.Append("FROM sync.TaskInfo a LEFT JOIN sync.TaskVersion b ON  a.VersionID = b.ID");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" )A ");
			return MSSQL.GetDataTableText(stringBuilder.ToString(), new SqlParameter[0]);
		}

		public JSOCTNetSDK.XmppTaskModel.TaskInfo GetModelByTaskCode(string TaskCode)
		{
			JSOCTNetSDK.XmppTaskModel.TaskInfo result = null;
			DataTable list = this.GetList(string.Format("TaskCode ='{0}' ", TaskCode));
			if (list.Rows.Count > 0)
			{
				result = this.DataRowToModel(list.Rows[0]);
			}
			return result;
		}

		private JSOCTNetSDK.XmppTaskModel.TaskInfo DataRowToModel(DataRow row)
		{
			JSOCTNetSDK.XmppTaskModel.TaskInfo taskInfo = new JSOCTNetSDK.XmppTaskModel.TaskInfo();
			if (row != null)
			{
				if (row["ID"] != null && row["ID"].ToString() != "")
				{
					taskInfo.ID = int.Parse(row["ID"].ToString());
				}
				if (row["TaskName"] != null)
				{
					taskInfo.TaskName = row["TaskName"].ToString();
				}
				if (row["TaskCode"] != null)
				{
					taskInfo.TaskCode = row["TaskCode"].ToString();
				}
				if (row["TaskType"] != null && row["TaskType"].ToString() != "")
				{
					taskInfo.TaskType = int.Parse(row["TaskType"].ToString());
				}
				if (row["ClassName"] != null)
				{
					taskInfo.ClassName = row["ClassName"].ToString();
				}
				if (row["DllPath"] != null)
				{
					taskInfo.DllPath = row["DllPath"].ToString();
				}
				if (row["CornStr"] != null)
				{
					taskInfo.CornStr = row["CornStr"].ToString();
				}
				if (row["VersionID"] != null && row["VersionID"].ToString() != "")
				{
					taskInfo.VersionID = int.Parse(row["VersionID"].ToString());
				}
				if (row["Enable"] != null && row["Enable"].ToString() != "")
				{
					if (row["Enable"].ToString() == "1" || row["Enable"].ToString().ToLower() == "true")
					{
						taskInfo.Enable = true;
					}
					else
					{
						taskInfo.Enable = false;
					}
				}
				if (row["IsCache"] != null && row["IsCache"].ToString() != "")
				{
					if (row["IsCache"].ToString() == "1" || row["IsCache"].ToString().ToLower() == "true")
					{
						taskInfo.IsCache = true;
					}
					else
					{
						taskInfo.IsCache = false;
					}
				}
				if (row["CacheTime"] != null && row["CacheTime"].ToString() != "")
				{
					taskInfo.CacheTime = int.Parse(row["CacheTime"].ToString());
				}
				if (row["IsSyncData"] != null && row["IsSyncData"].ToString() != "")
				{
					if (row["IsSyncData"].ToString() == "1" || row["IsSyncData"].ToString().ToLower() == "true")
					{
						taskInfo.IsSyncData = true;
					}
					else
					{
						taskInfo.IsSyncData = false;
					}
				}
			}
			return taskInfo;
		}
	}
}
